<template>
  <div class="page-container">
    <!-- 用户信息头部 -->
    <div class="user-header">
      <div class="user-info" @click="goToLogin" v-if="!userStore.isLoggedIn">
        <div class="avatar">
          <img src="https://picsum.photos/80/80?random=1" alt="默认头像" />
        </div>
        <div class="info">
          <h3 class="nickname">点击登录</h3>
          <p class="tip">登录后享受更多服务</p>
        </div>
        <ArrowRight class="arrow" />
      </div>
      
      <div class="user-info" v-else>
        <div class="avatar">
          <img :src="userStore.userInfo.avatar" :alt="userStore.userInfo.nickname" />
        </div>
        <div class="info">
          <h3 class="nickname">{{ userStore.userInfo.nickname }}</h3>
          <p class="phone">{{ userStore.userInfo.phone }}</p>
        </div>
        <ArrowRight class="arrow" @click="goToProfile" />
      </div>
    </div>

    <!-- 订单状态 -->
    <div class="order-status" v-if="userStore.isLoggedIn">
      <div class="status-header">
        <h3>我的订单</h3>
        <span class="more" @click="goToOrder">查看全部 ></span>
      </div>
      <div class="status-grid">
        <div class="status-item" @click="goToOrder('pending')">
          <div class="status-icon">
            <img src="https://picsum.photos/40/40?random=1" alt="待付款" />
          </div>
          <span class="status-text">待付款</span>
          <span class="status-count" v-if="orderCounts.pending">{{ orderCounts.pending }}</span>
        </div>
        <div class="status-item" @click="goToOrder('processing')">
          <div class="status-icon">
            <img src="https://picsum.photos/40/40?random=1" alt="待发货" />
          </div>
          <span class="status-text">待发货</span>
          <span class="status-count" v-if="orderCounts.processing">{{ orderCounts.processing }}</span>
        </div>
        <div class="status-item" @click="goToOrder('shipped')">
          <div class="status-icon">
            <img src="https://picsum.photos/40/40?random=1" alt="待收货" />
          </div>
          <span class="status-text">待收货</span>
          <span class="status-count" v-if="orderCounts.shipped">{{ orderCounts.shipped }}</span>
        </div>
        <div class="status-item" @click="goToOrder('completed')">
          <div class="status-icon">
            <img src="https://picsum.photos/40/40?random=1" alt="待评价" />
          </div>
          <span class="status-text">待评价</span>
          <span class="status-count" v-if="orderCounts.completed">{{ orderCounts.completed }}</span>
        </div>
      </div>
    </div>

    <!-- 功能菜单 -->
    <div class="menu-section">
      <div class="menu-group">
        <div class="menu-item" @click="goToAddress">
          <div class="menu-icon">
            <img src="https://picsum.photos/24/24?random=1" alt="收货地址" />
          </div>
          <span class="menu-text">收货地址</span>
          <ArrowRight class="arrow" />
        </div>
        <div class="menu-item" @click="goToFavorites">
          <div class="menu-icon">
            <img src="https://picsum.photos/24/24?random=1" alt="我的收藏" />
          </div>
          <span class="menu-text">我的收藏</span>
          <ArrowRight class="arrow" />
        </div>
        <div class="menu-item" @click="goToCoupons">
          <div class="menu-icon">
            <img src="https://picsum.photos/24/24?random=1" alt="优惠券" />
          </div>
          <span class="menu-text">优惠券</span>
          <ArrowRight class="arrow" />
        </div>
      </div>

      <div class="menu-group">
        <div class="menu-item" @click="goToCustomerService">
          <div class="menu-icon">
            <img src="https://picsum.photos/24/24?random=1" alt="客服中心" />
          </div>
          <span class="menu-text">客服中心</span>
          <ArrowRight class="arrow" />
        </div>
        <div class="menu-item" @click="goToSettings">
          <div class="menu-icon">
            <img src="https://picsum.photos/24/24?random=1" alt="设置" />
          </div>
          <span class="menu-text">设置</span>
          <ArrowRight class="arrow" />
        </div>
        <div class="menu-item" @click="goToAbout">
          <div class="menu-icon">
            <img src="https://picsum.photos/24/24?random=1" alt="关于我们" />
          </div>
          <span class="menu-text">关于我们</span>
          <ArrowRight class="arrow" />
        </div>
      </div>

      <div class="menu-group" v-if="userStore.isLoggedIn">
        <div class="menu-item logout" @click="handleLogout">
          <div class="menu-icon">
            <img src="https://picsum.photos/24/24?random=1" alt="退出登录" />
          </div>
          <span class="menu-text">退出登录</span>
          <ArrowRight class="arrow" />
        </div>
      </div>
    </div>

    <!-- 推荐商品 -->
    <div class="recommend-section">
      <h3 class="section-title">为您推荐</h3>
      <div class="recommend-list">
        <div 
          class="recommend-item" 
          v-for="product in recommendProducts" 
          :key="product.id"
          @click="goToProduct(product.id)"
        >
          <div class="product-image">
            <img :src="product.image" :alt="product.name" />
          </div>
          <div class="product-info">
            <h4 class="product-name">{{ product.name }}</h4>
            <div class="product-price">
              <span class="current-price">¥{{ product.price }}</span>
              <span class="original-price" v-if="product.originalPrice">¥{{ product.originalPrice }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ArrowRight } from '@nutui/icons-vue'

const router = useRouter()
const userStore = useUserStore()

// 订单数量统计
const orderCounts = ref({
  pending: 2,
  processing: 1,
  shipped: 0,
  completed: 3
})

// 推荐商品
const recommendProducts = ref([
  {
    id: 11,
    name: '智能手表',
    price: 599,
    originalPrice: 799,
    image: 'https://picsum.photos/80/80?random=1'
  },
  {
    id: 12,
    name: '无线充电宝',
    price: 129,
    image: 'https://picsum.photos/80/80?random=1'
  },
  {
    id: 13,
    name: '蓝牙音箱',
    price: 199,
    originalPrice: 299,
    image: 'https://picsum.photos/80/80?random=1'
  }
])

// 跳转到登录页
const goToLogin = () => {
  router.push('/login')
}

// 跳转到个人资料
const goToProfile = () => {
  // 这里可以跳转到个人资料编辑页
  console.log('跳转到个人资料页')
}

// 跳转到订单页
const goToOrder = (status = '') => {
  if (!userStore.isLoggedIn) {
    goToLogin()
    return
  }
  router.push({ path: '/order', query: { status } })
}

// 跳转到收货地址
const goToAddress = () => {
  if (!userStore.isLoggedIn) {
    goToLogin()
    return
  }
  router.push('/address-list')
}

// 跳转到我的收藏
const goToFavorites = () => {
  if (!userStore.isLoggedIn) {
    goToLogin()
    return
  }
  router.push('/favorites')
}

// 跳转到优惠券
const goToCoupons = () => {
  if (!userStore.isLoggedIn) {
    goToLogin()
    return
  }
  router.push('/coupons')
}

// 跳转到客服中心
const goToCustomerService = () => {
  console.log('跳转到客服中心')
}

// 跳转到设置
const goToSettings = () => {
  console.log('跳转到设置页')
}

// 跳转到关于我们
const goToAbout = () => {
  console.log('跳转到关于我们页')
}

// 跳转到商品详情
const goToProduct = (productId) => {
  router.push(`/product-detail/${productId}`)
}

// 退出登录
const handleLogout = () => {
  userStore.logout()
  // 显示提示
  showToast('已退出登录')
}

// 显示提示
const showToast = (message) => {
  console.log(message)
}

onMounted(() => {
  // 页面加载完成后的逻辑
})
</script>

<style scoped>
.user-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 24px 16px;
  color: white;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 16px;
  cursor: pointer;
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  overflow: hidden;
  border: 3px solid rgba(255, 255, 255, 0.3);
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.info {
  flex: 1;
}

.nickname {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 8px 0;
}

.phone, .tip {
  font-size: 14px;
  margin: 0;
  opacity: 0.9;
}

.arrow {
  color: rgba(255, 255, 255, 0.8);
  font-size: 20px;
}

.order-status {
  background: white;
  margin: 12px 0;
  padding: 20px 16px;
}

.status-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.status-header h3 {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.more {
  font-size: 12px;
  color: #666;
  cursor: pointer;
}

.status-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
}

.status-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  position: relative;
}

.status-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
}

.status-icon img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.status-text {
  font-size: 12px;
  color: #333;
  text-align: center;
}

.status-count {
  position: absolute;
  top: -5px;
  right: -5px;
  background: #ff4757;
  color: white;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 10px;
  min-width: 18px;
  text-align: center;
}

.menu-section {
  background: white;
  margin-bottom: 12px;
}

.menu-group {
  border-bottom: 1px solid #f0f0f0;
}

.menu-group:last-child {
  border-bottom: none;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.menu-item:hover {
  background: #f8f8f8;
}

.menu-item.logout {
  color: #ff4757;
}

.menu-icon {
  width: 24px;
  height: 24px;
  flex-shrink: 0;
}

.menu-icon img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.menu-text {
  flex: 1;
  font-size: 14px;
  color: #333;
}

.menu-item.logout .menu-text {
  color: #ff4757;
}

.arrow {
  color: #ccc;
  font-size: 16px;
}

.recommend-section {
  background: white;
  padding: 16px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin: 0 0 16px 0;
}

.recommend-list {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.recommend-item {
  cursor: pointer;
  text-align: center;
}

.recommend-item .product-image {
  width: 100%;
  height: 80px;
  margin-bottom: 8px;
}

.recommend-item .product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 4px;
}

.recommend-item .product-name {
  font-size: 12px;
  color: #333;
  margin: 0 0 4px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.recommend-item .product-price {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.recommend-item .current-price {
  font-size: 14px;
  font-weight: 600;
  color: #ff4757;
}

.recommend-item .original-price {
  font-size: 10px;
  color: #999;
  text-decoration: line-through;
}
</style>
